const app = getApp();
const https = require('../../utils/https');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order_id: null,
    goods_list: [],
    source_list: [],
    source_index: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options && options.order_id) {
      this.setData({
        order_id: options.order_id
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.getSourceList();
    this.getOrderInfo();
    this.getGoodsList();
  },

  goDetail() {
    wx.navigateTo({
      url: `/pages/myorder_info/info?order_sn=${this.data.order_info.order_sn}`
    })
    // this.saveSourceList().then(() => {
    //
    // }).catch(() => {
    //
    // })

  },
  goHome() {
    wx.switchTab({
      url: '/pages/home/home'
    })
    // this.saveSourceList().then(() => {
    //
    // }).catch(() => {
    //
    // })
  },
  bindPickerChange(e) {
    console.log(e.detail.value)
    this.setData({
      source_index: e.detail.value
    })
  },

  //获取订单数据 支付成功的
  getOrderInfo() {
    var that = this,
      order_sn = app.globalData.order_sn ? app.globalData.order_sn : '200818155139000004';
    https.post('getOrderInfo', {
      order_sn: order_sn
    }, false, '正在查询').then(res => {
      that.setData({
        order_info: res.order_info
      })
    })
  },
  //获取用户来源列表
  getSourceList() {
    var that = this;
    https.post('getSource', null, false, '').then(res => {
      console.log(res)
      that.setData({
        source_list: res
      })
    })
  },
  // 保存用户来源
  saveSourceList() {
    var that = this;
    const {order_id, source_list, source_index} = this.data;

    return new Promise((success, fail) => {
      if(!source_index) {
        wx.showToast({
          title: '请选择预约途径',
          icon: 'none',
          duration: 1500
        })
        return fail();
      }
      https.post('saveSource', {
        book_order_id: order_id,
        source_id: source_list[source_index].id
      }, false, '').then(res => {
        success()
      }).catch(() => {
        fail()
      })
    })

  },
  getGoodsList() {
    var that = this;
    https.post('getGoods', {
      pagesize: 12,
      is_tj: 1,
      is_page: 0
    }, true).then(res => {
      that.setData({
        goods_list: res.list
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onShow();
  },

})